package com.csweimei.rentmanagementtreasure.weight;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;

import androidx.core.content.ContextCompat;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.EntDealChatBean;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.gtdev5.geetolsdk.mylibrary.util.DensityUtils;
import com.gtdev5.geetolsdk.mylibrary.util.LogUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class BarChatManger {

    private BarChart barChart;

    List<BarEntry> entries;
    List<String> xList = new ArrayList<>();

    public BarChatManger(Context context,BarChart mBarChart) {
        this.barChart = mBarChart;
        initBar(context);

    }

    private void initBar(Context context) {
        barChart.setNoDataText("暂无图表数据");
        barChart.setNoDataTextColor(ContextCompat.getColor(context, R.color.black));
        Paint paint = barChart.getPaint(Chart.PAINT_INFO);
        paint.setTextSize(DensityUtils.Dp2Px(context, 14));

    }

    private void initX() {
        // 1. 获取X轴对象
        XAxis xAxis = barChart.getXAxis();

// 2. 设置X轴标签的位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

// 3. 设置X轴标签的字体大小和颜色
        xAxis.setTextSize(10f);
        xAxis.setTextColor(Color.BLACK);

// 4. 设置X轴标签的旋转角度
        xAxis.setLabelRotationAngle(-90f);

// 5. 设置X轴标签的最大显示数量
        xAxis.setLabelCount(xStr.length);

// 6. 设置X轴的网格线是否显示
        xAxis.setDrawGridLines(false);

// 7. 设置X轴的轴线是否显示
        xAxis.setDrawAxisLine(false);

// 8. 设置X轴的轴标签是否显示
        xAxis.setEnabled(true);

// 9. 设置X轴的轴标签
//        xAxis.setValueFormatter(new IndexAxisValueFormatter(new String[]{"互通的石头肉"}));
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xStr));
    }

    String[] xStr;

    public void setData(List<EntDealChatBean.DataDTO> data) {
        entries = new ArrayList<>();
        LogUtils.i(data.toString());
        for (int i = 0; i < data.size(); i++) {
            entries.add(new BarEntry(i, data.get(i).getDealCount()));
            xList.add(data.get(i).getName());
        }

//        xStr = xList.toArray(new String[data.size()]);

        // 图表绑定数据，设置图表折现备注
        BarDataSet barDataSet = new BarDataSet(entries, "成交房源数");
        BarData barData = new BarData(barDataSet);
        barData.setBarWidth(0.05f);
        //图表标题
        Description description = barChart.getDescription();
        description.setText("");
        // 图表绑定数据值
        barChart.setData(barData);


//        initX();


        // 刷新图表
        barChart.invalidate();

    }


    //设置Y轴刻度样式
    public class MyYAxisValueFormatter extends ValueFormatter {

        private DecimalFormat mFormat;

        public MyYAxisValueFormatter() {
            mFormat = new DecimalFormat("###,###,###,##0");
        }

        @Override
        public String getFormattedValue(float value) {
            return mFormat.format(value) + "k";
        }
    }


}
